import router from './router'
import store from './store'
const whiteList = ['/login', '404']
import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css' // 引入进度条样式
// 导航守卫
// 前置守卫
// next() 放过
// next(false) 停止跳转
// next(路径) 页面跳转
router.beforeEach(async(to, from, next) => {
  NProgress.start() // 开启进度条
  // have token ? is to /login ?
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userId) {
        // no userInfo to get
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    // no token  is in whiteList
    // whiteList don't need token ,so next
    // other need token .so to login for token
    if (whiteList.indexOf(to.path) > -1) {
      // 在白名单里
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done() // 手动强制关闭一次  为了解决 手动切换地址时  进度条的不关闭的问题
})
// 后置守卫
router.afterEach(() => {
  NProgress.done() // 关闭进度条
})
